<?php namespace Home\Controller;
	
//	用户类控制器
	class UserController extends CommonController{
		
		public function __init(){
			parent::__init();
		}
		
		public function index(){
//			p($_SESSION);die;
//			获取当前登录的账户
			$uid = Q('session.uid',0,'intval');
//			p($uid);die;
//			查询当前用户信息
			$userData = Db::table('snshop_user')
						-> where('uid',$uid)
						-> first();
//			p($userData);die;
			View::with('userData',$userData);
			
			
			
			View::make();
		}
		
//		修改
		public function edit(){
//			p($_POST);die;
			
//			传入模型中处理
			$userModel = new \Common\Model\User;
			$result = $userModel -> edit();
			$errors = $userModel -> getError();
//			 $errors;die;
//			转化为字符串
			$errors = strval($errors);
			
			$data = array('result' => $result,'errors' => $errors);
			View::ajax($data);
			
		}
		
		
//		订单表
		public function orderList(){
			
			$uid = Q('session.uid');
			$status = Q('get.status','');
//			p($uid);die;
			if($status == '待付款'){
	//			查询订单表和订单列表分配到前台
				$orderData = Db::table('snshop_order')
							-> where('uid',$uid)
							-> where('status','未付款')
							-> get();
				foreach($orderData as $k => $v){
					$orderData[$k]['lists'] = Db::table('snshop_order_list l')
										-> join('snshop_goods g','l.snshop_goods_gid','=','g.gid')
										-> where('snshop_order_oid',$v['oid'])
										-> get();
					
					
					
				}
			}elseif($status == '待收货'){
					//			查询订单表和订单列表分配到前台
				$orderData = Db::table('snshop_order')
							-> where('uid',$uid)
							-> where('status','已发货')
							-> get();
				
				
				
				foreach($orderData as $k => $v){
					$orderData[$k]['lists'] = Db::table('snshop_order_list l')
										-> join('snshop_goods g','l.snshop_goods_gid','=','g.gid')
										-> where('snshop_order_oid',$v['oid'])
										-> get();
					
					
					
				}
			}elseif($status == '待评价'){
					//			查询订单表和订单列表分配到前台
				$orderData = Db::table('snshop_order')
							-> where('uid',$uid)
							-> where('status','已完成')
							-> get();
				foreach($orderData as $k => $v){
					$orderData[$k]['lists'] = Db::table('snshop_order_list l')
										-> join('snshop_goods g','l.snshop_goods_gid','=','g.gid')
										-> where('snshop_order_oid',$v['oid'])
										-> get();
					
					
					
				}
			}elseif($status == '待发货'){
					//			查询订单表和订单列表分配到前台
				$orderData = Db::table('snshop_order')
							-> where('uid',$uid)
							-> where('status','待发货')
							-> get();
				foreach($orderData as $k => $v){
					$orderData[$k]['lists'] = Db::table('snshop_order_list l')
										-> join('snshop_goods g','l.snshop_goods_gid','=','g.gid')
										-> where('snshop_order_oid',$v['oid'])
										-> get();
					
					
					
				}
			}else{
					//			查询订单表和订单列表分配到前台
				$orderData = Db::table('snshop_order')
							-> where('uid',$uid)
							-> get();
				foreach($orderData as $k => $v){
					$orderData[$k]['lists'] = Db::table('snshop_order_list l')
										-> join('snshop_goods g','l.snshop_goods_gid','=','g.gid')
										-> where('snshop_order_oid',$v['oid'])
										-> get();
					
					
					
				}
			}
			
//			p($orderData);die;
			View::with('orderData',$orderData);
			
//			获取status相关数量
			$statusGroup = array();
			$statusGroup['fahuo'] = Db::table('snshop_order')
							-> where('status','待发货')
							-> count();
			$statusGroup['souhuo'] = Db::table('snshop_order')
							-> where('status','已发货')
							-> count();
			$statusGroup['weifukuan'] = Db::table('snshop_order')
							-> where('status','未付款')
							-> count();
			$statusGroup['wancheng'] = Db::table('snshop_order')
							-> where('status','已完成')
							-> count();
							
//			p($statusGroup);die;
			View::with('statusGroup',$statusGroup);
			
			
			
			View::make();
		}
		
		
		
//		订单详情
		public function OrderDetail(){
//			p($_GET);die;
			$oid = Q('get.oid',0,'intval');
			$olid = Q('get.olid',array());
			
//			查询订单表和订单表
			$orderData = Db::table('snshop_order')
							-> where('oid',$oid)
							-> first();
							
			View::with('orderData',$orderData);				
//			p($orderData);die;
			$orderListData = array();
			foreach($olid as $k => $v){
				$orderListData[] = Db::table('snshop_order_list l')
								-> join('snshop_goods g','snshop_goods_gid','=','gid')
								-> where('olid',$v)
								-> first();
			}
			
			View::with('orderListData',$orderListData);				
//			p($orderListData);die;
			View::make();
		}
		
		
//		删除订单
		public function del(){
//			p($_GET);die;
			$oid = Q('get.oid',0,'intval');
			$olid = Q('get.olid',array());
			foreach($olid as $k => $v){
				Db::table('snshop_order_list') -> where('olid',$v) -> delete();
			}
			Db::table('snshop_order') -> where('oid',$oid) -> delete();
			
			View::success('删除成功',U('orderList'));
			
		}
		
	//	确认收货方法
		public function trueGet(){
//			p($_GET);die;
			$oid = Q('get.oid',0,'intval');
			
			$result = Db::table('snshop_order') -> where('oid',$oid) -> update(array('status'=>'已完成'));
//			p($result);die;
			if(!$result){
				View::error('确认收货失败，出现未知错误');
			}
			View::success('确认收货成功',U('orderList'));
		}
		
		
//		评价
		public function evaluate(){
//			p($_GET);die;
			$olid = Q('get.olid',0,'iintval');
//			获得该商品的id
			$gid = Db::table('snshop_order_list')
					-> where('olid',$olid)
					-> pluck('snshop_goods_gid');
//			p($gid);die;
//			跳转到这条商品下的评价页面
			$str = <<<END
			<script type="text/javascript">
				location.href="index.php?m=home&c=Content&a=index&gid={$gid}";
			</script>
END;
			echo $str;die;
			
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	?>